/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BAStandardResultObject.m
//  redia-bapps-ios
//
//  Created by Henrik Lynbech on 14/08/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BAStandardResultObject.h"
#import "JSONEncodingHelperMethods.h"

@implementation BAStandardResultObject

+ (id)objectWithJSONObject:(NSDictionary *)jsonObj
{
    BAStandardResultObject *obj = [BAStandardResultObject new];
    
    obj.result = [JSONEncodingHelperMethods parseBoolWithKey:@"result" fromJsonObject:jsonObj];

    obj.code = [JSONEncodingHelperMethods parseIntegerWithKey:@"code" fromJsonObject:jsonObj];
    
    obj.jsonData = [jsonObj objectForKey:@"data"];
    
    obj.language = [JSONEncodingHelperMethods parseStringWithKey:@"language" fromJsonObject:jsonObj];
    EXPECT_OBJECT(NSString, obj.language);
    obj.message = [JSONEncodingHelperMethods parseStringWithKey:@"message" fromJsonObject:jsonObj];
    EXPECT_OBJECT(NSString, obj.message);
    
    return obj;
}


-(NSDictionary *)toJSONObject{
    DLog(@"Not implemented");
    return nil;
}

-(BOOL)parseDataFieldToObjectOfClass:(Class<JSONEncoding>)class_object
{
    if (self.result) {
        //only parse if RPC result was succesful
        if (self.jsonData != nil || self.jsonData != [NSNull null]) {
            self.parsedData = [JSONEncodingHelperMethods parseObjectOfClass:class_object fromJsonObject:self.jsonData];
        }
        self.jsonData = nil;
        return TRUE;
    }
    
    return FALSE;
}
@end
